package com.example.system.class02;

/**
 * @Date 2022/7/18
 * @Created by Jonathan
 */
public class Code06_FirstLessNumIndex {
    public static void main(String[] args) {

        int[] arr = new int[]{1, 2, 3, 5, 7, 9, 11};
        System.out.println(firstLessThanNumIndex(arr, 2));
        System.out.println(firstLessThanNumIndex(arr, 8));
    }


    private static int firstLessThanNumIndex(int[] arr, int num) {
        // -1 表示不存在
        if (arr == null || arr.length < 1 || arr[arr.length - 1] <= num) {
            return -1;
        }
        int L = 0;
        int R = arr.length - 1;
        int index = -1;
        while (L <= R) {
            int M = L + ((R - L) >> 1);
            // 要找第一个比num小的索引下标

            if (arr[M] > num) {
                R = M - 1;
            } else {
                L = M + 1;
                index = M;
            }
        }
        return index;
    }
}
